package com.bazinga.contact.view;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.controller.UserDetailsManager;
import com.bazinga.contact.model.UserDetails;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class RegisterationAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 9149826260758390091L;
	private UserDetails userDetails;
	private Long id;
	private Map<String, Object> session;
	String userType = null;

	private UserDetailsManager linkController;

	public RegisterationAction() {
		linkController = new UserDetailsManager();
	}

	public String execute() {
		return SUCCESS;
	}

	public String add() {
		System.out.println(getUserDetails());
		try {
			linkController.add(getUserDetails());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.session = ActionContext.getContext().getSession();
		session.put("logged-in", "true");
  	  	session.put("username", userDetails.getEmail());
  	  	session.put("firstname", userDetails.getFname());
  	  	session.put("userid", userDetails.getUserid());
  	  	if(userDetails.isIs_student())
  	  		userType = "student";
  	  	else
  	  		userType = "employer";
  	  	
  	  	session.put("usertype", userType);
  	  	return userType;
	}

	public String delete() {
		linkController.delete(getId());
		return SUCCESS;
	}



	public UserDetails getUserDetails() {
		return userDetails;
	}

	public void setUserDetails(UserDetails userDetails) {
		this.userDetails = userDetails;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
}
